<template>
  <div class="mem">
    <div></div>
    <div>请通过站点网址访问111</div>
    <div>手机{{phone}}</div>

    <div>油企{{enterpriseId}}</div>

    <div>unionId {{unionId}}</div>

    <div>openId {{openId}}</div>
  </div>
</template>

<script>
import ProductList from "@/components/ProductList";
import { mapState, mapGetters, mapActions } from "vuex";
import wx from "weixin-js-sdk";
import Vue from "vue";
import { Toast, Button, Popup, NumberKeyboard } from "vant";
import { ActionSheet  } from "vant";
import moment from 'moment'

Vue.use(Popup);
Vue.use(Toast);
Vue.use(Button);
Vue.use(NumberKeyboard);
Vue.use(ActionSheet);


export default {
  head() {
    return {
      title: "开通超级会员卡",
    };
  },
  data() {
    return {
      mask: false,
      cardList: [],
      chargeItem: {},
      chargeAmount: "",
      stationInfo: {},
      payLoading: false,
      show: false,
      discountList: [],
      cardOilsTypeClass: [, "gasoline", "diesel", "lng", "cng"],
      cardOilsTypeText: [, "汽油卡", "柴油卡", "LNG卡", "CNG卡"],
      tempEnterpriseId: undefined,
      a: undefined,
      b: undefined,
      c: undefined,
      d: undefined,
      e: undefined,
      stataionShow: false,
      stationActions: [],
      selectedStation: {},
    };
  },
  created() {
    // this.a = this.$route.query.enterpriseId;
    // this.b = sessionStorage.getItem("enterpriseId");

    // this.tempEnterpriseId =
    //   this.$route.query.enterpriseId || sessionStorage.getItem("enterpriseId");
    // this.c = this.tempEnterpriseId;

    // sessionStorage.setItem("enterpriseId", this.tempEnterpriseId);

    // const unionId = this.unionId;

    // if (!this.tempEnterpriseId) {
    //   alert("此页面不支持直接访问");
    //   return;
    // }

    // this.init();
    // this.pay();
    if(this.phone){
      
    }
    
    
  },
  methods: {
    ...mapActions({
      getSdkConfig: "authen/getSdkConfig",
    }),
    onStationSelect(q) {
      this.selectedStation = q;
      this.mask = !this.mask;
    },
    switchMask() {
      this.mask = !this.mask;
    },
    getDiscount() {
      this.discountList = [];
      // 拉取折扣
      this.$axios
        .$get("/getCardRechargeSettingList", {
          params: {
            enterpriseId: this.tempEnterpriseId,
            cardOilsType: this.chargeItem.cardOilsType,
          },
        })
        .then((res) => {
          if (res.retCode === 0) {
            this.discountList = res.data[0].customerCardSettingDetailList;
          }
        });
    },

    goCharge(ele) {
      this.stataionShow = true;
      this.chargeItem = ele;
      this.getDiscount();
    },
    charge() {
      this.chargeAmount = this.chargeAmount.toString().replace(/\.$/g, "");
      this.pay();
    },
    init() {
      if(!this.unionId){
        return
      }
      
      this.$axios
        .$get("/selectStaInfoList", {
          params: {
            enterpriseId: this.tempEnterpriseId,
          },
        })
        .then((res) => {
          if (res.retCode === 0) {
            if (!!res.data) {
              this.stationActions = res.data.map((ele) => {
                return { ...ele, name: ele.stationName };
              });
            }
          }
        });
      // 获取卡片
      this.$axios
        .$get("/getElectronicCardList", {
          params: {
            mobilePhone: this.phone,
            enterpriseId: this.tempEnterpriseId,
          },
        })
        .then((res) => {
          if (res.retCode === 0) {
            this.cardList = res.data;
            console.log(this.cardList);
          }
        });
    },
    async pay() {
      try {
        const noncestr = Math.random().toString(36).slice(2);
        // const noncestr = "15klkdfda";

        const timestamp = (+ moment.utc()).toString().slice(0,10);
        
        let signature = '';
        let cardId = 'pJR5R6hA0H4y0VKrmXIEPdCGcKD8';
        let cardApiTicket = '';
        this.$axios
        .$get("/getCardSignature", {
          params: {
            timestamp: timestamp,
            noncestr: noncestr,
            enterpriseId: 7,
            cardId: cardId
          },
        })
        .then((res) => {
          if (res.retCode === 0) {
            cardApiTicket = res.data.cardApiTicket
            signature = res.data.signature
          }
        });
        this.getSdkConfig({
          jsApiList: ['addCard','chooseCard','openCard'],
        }).then((res) => {
          console.log(2);
          wx.config(res);
          console.log(3)
          
          wx.ready(function () {
            console.log(4)
            let cardExt = {
              nonce_str: noncestr,
              timestamp: timestamp,
              signature: signature
              // api_ticket: cardApiTicket,
              // card_id: cardId
            } 
            cardExt = JSON.stringify(cardExt)
            console.log(cardExt);
            wx.addCard({
              cardList:[{
                cardId:cardId,
                cardExt:cardExt
              }],
            
              success: function (res) {
                console.log('成功',res)
                alert("支付完成");
              },
              cancel: function (err) {
                alert("您取消了付款");
              }
            });
          });
          wx.error(function (res) {
            alert("拉取微信Sdk配置出错");
          });
        });
      } catch (err) {
        this.payLoading = false;
        alert(err);
      }
    },

    selectDiscount(discount) {
      this.chargeAmount = discount;
    },
  },
};
</script>

<style lang="scss">
</style>